package com.lsa.southwind.controller;


import com.lsa.southwind.entity.DormitoryAdmin;
import com.lsa.southwind.form.RuleForm;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.service.DormitoryAdminService;
import com.lsa.southwind.util.ResultVoUtil;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dormitoryAdmin")
public class DormitoryAdminController {
    @Autowired
    private DormitoryAdminService dormitoryAdminService;


    @GetMapping("/login")
    //接收前端的ruleForm对象
    public ResultVo login(RuleForm ruleForm) {
        ResultVo resultVologin = dormitoryAdminService.login(ruleForm);
        return resultVologin;
    }

    @PostMapping("/save")
    public ResultVo save(@RequestBody DormitoryAdmin dormitoryAdmin) {
        /*通过Service自带的增加方法去增加不用再去写Mapper*/
        boolean save = this.dormitoryAdminService.save(dormitoryAdmin);
        if (!save) {
            ResultVo fail = ResultVoUtil.fail();
            return fail;
        } else {
            ResultVo success = ResultVoUtil.success(null);
            return success;
        }
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVo list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageVo pageVo = this.dormitoryAdminService.list(page, size);
        return ResultVoUtil.success(pageVo);

    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm) {
        PageVo search = this.dormitoryAdminService.search(searchForm);

        return ResultVoUtil.success(search);
    }

    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id) {

        DormitoryAdmin dormitoryAdmin = this.dormitoryAdminService.getById(id);
        return ResultVoUtil.success(dormitoryAdmin);

    }

    @PutMapping("/update")
    public ResultVo update(@RequestBody DormitoryAdmin dormitoryAdmin) {
        boolean update = this.dormitoryAdminService.updateById(dormitoryAdmin);
        if (!update) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVo delete(@PathVariable("id") Integer id) {
        boolean removeById = this.dormitoryAdminService.removeById(id);
        if (!removeById) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }

    @GetMapping("/list")
    public ResultVo list() {
        List<DormitoryAdmin> list = this.dormitoryAdminService.list();
        return ResultVoUtil.success(list);
    }
}

